package org.wiztools.te.plugin;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Properties;
import java.util.Set;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

import org.wiztools.te.TEMod;
import org.wiztools.te.TEProcess;

public class SystemProperties implements TEProcess{
	private TEMod tem;
	private Container ct;
	private JList jl;
	private DefaultListModel dlm;
	private JButton jb_view, jb_ok, jb_cancel;
	private JDialog jd;
	private JTextField jtf_jd_property, jtf_jd_value;
	private final String noSelectionMadeWarning = "Please Select A Property!";

	public void setTEMod(TEMod tem){
		this.tem = tem;
	}
	
	public void init(){

		createJD();

		ct = new JPanel();
		ct.setLayout(new BorderLayout());

		dlm = new DefaultListModel();
		jl = new JList(dlm);
		jl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		ct.add(new JScrollPane(jl), BorderLayout.CENTER);

		JPanel jp_south = new JPanel();
		jp_south.setLayout(new FlowLayout());
		jb_view = new JButton("View");
		jb_view.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent ae){
					String str = (String)jl.getSelectedValue();
					if(str==null){
						JOptionPane.showMessageDialog(tem.getJD(), noSelectionMadeWarning, noSelectionMadeWarning, JOptionPane.ERROR_MESSAGE);
						return;
					}
					String value = System.getProperty(str);
					if(value.equals("\n"))value = "\\n";
					else if(value.equals("\r\n"))value = "\\r\\n";
					else if(value.equals("\r"))value = "\\r";
					jtf_jd_property.setText(str);
					jtf_jd_value.setText(value);
					JDialog jf = tem.getJD();
					Point p = jf.getLocation();
					jd.setLocation(p.x+(jf.getWidth()-jd.getWidth())/2, p.y+(jf.getHeight()-jd.getHeight())/2);
					jd.setVisible(true);
					// JOptionPane.showMessageDialog(tem.getJD(), "Property: \""+str+"\" || Value: \""+value+"\"", "Property Value", JOptionPane.INFORMATION_MESSAGE);
				}
			}
		);
		jb_view.setMnemonic('V');
		jb_ok = new JButton("OK!");
		jb_ok.setMnemonic('O');
		jb_ok.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent ae){
					String str = (String)jl.getSelectedValue();
					if(str==null){
						JOptionPane.showMessageDialog(tem.getJD(), noSelectionMadeWarning, noSelectionMadeWarning, JOptionPane.ERROR_MESSAGE);
						return;
					}
					tem.insert("System.getProperty(\""+str+"\")");
					tem.closeDialog();
				}
			}
		);
		jb_cancel = new JButton("Cancel");
		jb_cancel.setMnemonic('C');
		jb_cancel.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent ae){
					tem.closeDialog();
				}
			}
		);
		jp_south.add(jb_view);
		jp_south.add(jb_ok);
		jp_south.add(jb_cancel);

		ct.add(jp_south, BorderLayout.SOUTH);
	}
	
	private void createJD(){

		jd = new JDialog(tem.getJD(), "Property/Value");

		Container ct = jd.getContentPane();
		ct.setLayout(new BorderLayout());
		JPanel jp_center = new JPanel();
		jp_center.setLayout(new BorderLayout());
		
		JPanel jp_center_west = new JPanel();
		jp_center_west.setLayout(new GridLayout(2, 1, 5, 5));
		jp_center_west.add(new JLabel("Property: "));
		jp_center_west.add(new JLabel("Value: "));

		jp_center.add(jp_center_west, BorderLayout.WEST);

		JPanel jp_center_center = new JPanel();
		jp_center_center.setLayout(new GridLayout(2, 1, 5, 5));
		jtf_jd_property = new JTextField(25);
		jtf_jd_property.setEditable(false);
		jtf_jd_value = new JTextField(25);
		jtf_jd_value.setEditable(false);
		jp_center_center.add(jtf_jd_property);
		jp_center_center.add(jtf_jd_value);

		jp_center.add(jp_center_center, BorderLayout.CENTER);

		ct.add(jp_center, BorderLayout.CENTER);

		JButton jb = new JButton("OK!");
		jb.setMnemonic('O');
		jb.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent ae){
					jd.setVisible(false);
				}
			}
		);
		JPanel jp_south = new JPanel();
		jp_south.setLayout(new FlowLayout());
		jp_south.add(jb);
		
		ct.add(jp_south, BorderLayout.SOUTH);

		jd.pack();
		jd.setResizable(false);
	}

	private void populateList(){
		dlm.clear();
		Properties p = System.getProperties();
                Set<Object> s = p.keySet();
                Object[] arr = s.toArray();
                Arrays.sort(arr);
		for(Object o: arr){
                    dlm.addElement(o);
		}
	}

	public void action(){
		populateList();
		tem.dispDialog(ct, "System.getProperty()");
	}
	
	public void destroy(){
		// do nothing!
	}
}
